<?php

/*
 * \brief Content for administration page including theme roller and javaScript
 * framework test unit options.
 * \author Salman Noor
 */

class admin_page_handler {

    /**
     * \brief Object to be able use the html_builder class functionality.
     */
    private $html_buillder;
    /**
     * \brief Object reading XML theme roller file.
     */
    private $xml_t_roller;
    /**
     * \brief Object writing to XML theme roller file.
     */
    private $xml_t_roller_editor;
    /**
     * \brief Variable string to store chosen theme from theme roller.
     */
    private $chosen_theme;

    /**
     * \brief Constructor instatiating private data members of the class
     * admin_page_handler.
     */
    public function __construct() {
        $this->html_buillder = new html_elements_builder();

        $this->xml_t_roller = simplexml_load_file("ThemeRoller.xml")
                or die("Error: Cannot create object");
        $this->getCurrentTheme();
        $this->loadXMLFileString();
    }

    /**
     * \brief Private member function reading from the theme roller XML file
     * and gets the current stored theme.
     * \note the file being read is ThemeRoller.xml
     */
    private function getCurrentTheme() {
        foreach ($this->xml_t_roller->children() as $themes) {
            foreach ($themes->children() as $theme => $data) {
                $this->chosen_theme = $data->name;
            }
        }
    }

    /**
     * \brief Memeber function that opens up the write handle for the theme
     * roller XML file.
     * \note the file being read is ThemeRoller.xml
     */
    private function loadXMLFileString() {
        $file = "ThemeRoller.xml";
        $fp = fopen($file, "rb") or die("cannot open theme roller xml file");
        $str = fread($fp, filesize($file));
        $this->xml_t_roller_editor = new DOMDocument();
        $this->xml_t_roller_editor->formatOutput = true;
        $this->xml_t_roller_editor->preserveWhiteSpace = false;
        $this->xml_t_roller_editor->loadXML($str) or die("Error");
    }

    /**
     * \brief Private member function setting new Theme from the theme roller.
     * \brief It updates the new theme in the XML file.
     * \note the file being read is ThemeRoller.xml
     */
    public function setNewTheme($newTheme) {
/// get document element
        $root = $this->xml_t_roller_editor->documentElement;
        $fnode = $root->firstChild;

///get a node
        $ori = $fnode->childNodes->item(0);

        $name = $this->xml_t_roller_editor->createElement("name");
        $nameText = $this->xml_t_roller_editor->createTextNode("$newTheme");
        $name->appendChild($nameText);


        $theme = $this->xml_t_roller_editor->createElement("chosenTheme");
        $theme->appendChild($name);
        $fnode->replaceChild($theme, $ori);
        $this->xml_t_roller_editor->save('ThemeRoller.xml');
        "<xmp>NEW:\n" . $this->xml_t_roller_editor->saveXML() . "</xmp>";
        return true;
    }

    /**
     * \brief Memeber function building the html content  for the theme viewer.
     * \note the file being read is ThemeRoller.xml
     */
    private function buildThemeViewer() {
        $themeViewer = "<div id='themeViewerContainer'>";
        $themeViewer = "Date: <div id='datepicker'></div>";
        $themeViewer .="<div class='buttonViewer'>
        <button>Button with icon only</button>
        <button>Button with icon on the left</button>
        <button>Button with two icons</button>
        <button>Button with two icons and no text</button></div>";
        $themeViewer .="</div>";
        return $themeViewer;
    }

    /**
     * \brief Memeber function building html content allowing the user to
     * select and set themes.
     * \return built html content for theme roller
     */
    public function buildThemeRoller() {
        $cssThemes = directoryToArray("/var/www/stellogoogle/eventsmanager/css/", false);
        foreach ($cssThemes as $key => $theme) {
            if ($theme != ".svn") {
                $this->html_buillder->addOption($theme, $theme);
            }
        }

        if (isset($this->chosen_theme)) {
            $this->html_buillder->setSelected("$this->chosen_theme");
        }
        $themRoller = "<div id='themeRollerContainer' class='ui-widget-content ui-corner-all'style='border:1px solid #CCCCCC;padding:20px 20px 20px 20px;'> ";
        $themRoller .= "<h3>Theme Roller</h3>";
        $themRoller .= $this->html_buillder->buildDropDown('themeSelector', "Please Select a Theme:");
        $themRoller .= $this->html_buillder->buildButton("selectTheme", "Set Theme", "themeSelector");
        $themRoller .= "<div id='themeViewerContainer' class='ui-widget-content ui-corner-all'style='border:1px solid #CCCCCC;padding:20px 20px 20px 20px;'>";
        $themRoller .= "<h3>Theme Viewer</h3>";
        $themRoller .= $this->buildThemeViewer();
        $themRoller .= "<div id='themeLoaderContainer'></div>";
        $themRoller .= "</div>";
        $themRoller .= "</div>";
        return $themRoller;
    }

}

/**
 * \brief php standard function that loops through directories and loops
 * directory names.
 * \return An array of directory names.
 */
function directoryToArray($directory, $recursive) {
    $array_items = array();
    if ($handle = opendir($directory)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if (is_dir($directory . "/" . $file)) {
                    if ($recursive) {
                        $array_items = array_merge($array_items, directoryToArray($directory . "/" . $file, $recursive));
                    }
                    $file = $file;
                    $array_items[] = preg_replace("/\/\//si", "/", $file);
                } else {
                    $file = $file;
                    $array_items[] = preg_replace("/\/\//si", "/", $file);
                }
            }
        }
        closedir($handle);
    }
    return $array_items;
}
?>
